var $;
layui.use(["element", "layer"], function () {
    $ = layui.$;
        var element = layui.element,
        layer = layui.layer;
    var l_o = $(".left-menu"), tab = "top-tab", l_m = "left-menu", t_m = "top-menu";
    var mainHeight = $(window).height() - 60 - 41 - 44 - 10;
    element.on("nav(" + t_m + ")", function (data) {
        l_o.hide().eq(data.index()).show()
    });
    l_o.on("click", "li", function () {
        $(this).siblings().removeClass("layui-nav-itemed")
    });
    element.on("nav(" + l_m + ")", function (data) {
        var a_t = data.children("a"), id = a_t.data("id"), url = a_t.data("url"), title = a_t.html(),
            length = $(".layui-tab-title").children("li[lay-id='" + id + "']").length;
        if (!length) {
            var iframe = '<iframe src="' + url + '" style="height:' + mainHeight + 'px;"></iframe>';
            element.tabAdd(tab, {title: title, content: iframe, id: id})
        }
        element.tabChange(tab, id)
    });
    //切换top-tab标签是左侧菜单跟着改变--杨浩 2018-01-12
    element.on('tab(top-tab)', function (data) {
        if (data.elem.context.attributes != undefined) {
            var layid = data.elem.context.attributes[0].nodeValue;
            if ($("#menu-" + layid).length > 0 &&  layid != 1) {
                $("#menu-1").removeClass("layui-this");
                $('li.layui-nav-item').removeClass('layui-nav-itemed');
                $('.layui-nav-child').find("dd").removeClass("layui-this");
                $("#menu-" + layid).addClass("layui-this").parents('li.layui-nav-item').addClass('layui-nav-itemed');
            }else{
                //特殊-首页
                $('li.layui-nav-item').removeClass('layui-nav-itemed');
                $("#menu-"+layid).addClass("layui-this");
            }
            element.render('tab', 'top-tab');
        }
    });
    l_o.children("li:first").children("a:first").click();
    $(".menu-flexible").click(function () {
        $(".left-menu-all").toggle();
        $(".layui-body,.layui-footer").css("left", ($(".left-menu-all").is(":hidden")) ? "0" : "200px")
    });
    $(".layui-tab-button").on("click", "a", function () {
        var iframe = $(".layui-tab-content").find(".layui-show iframe");
        iframe.attr("src", iframe.attr("src"))
    })

    $(window).resize(function() {
        var mainHeight = $(window).height() - 60 - 41 - 44 - 10;
        $('.admin-tab-content > .layui-tab-item> iframe').height(mainHeight);
    });
});


/**
 * HTTP请求
 * @param url
 * @param params
 * @param method
 * @param async
 * @returns
 */
function http_request(url,params,method,async){
	var res;
	$.ajax({
		url:url,
		type:method,
		data:params,
		async:async,
		dataType:'json',
		success:function(msg){
			res=msg;
		}
	});
	return res;
}
function http_get(url,params){
	return http_request(url,params,'GET',false);
}
function http_post(url,params){
	return http_request(url,params,'POST',false);
}
function async_post(url,params){
	return http_request(url,params,'POST',true);
}
